package com.gitee.mrj.aiboot.core.sf.generators.ui;

import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.gitee.mrj.aiboot.annotation.config.KV;
import com.gitee.mrj.aiboot.annotation.config.sf.ui.SFOptionalHelp;
import com.gitee.mrj.aiboot.core.sf.generators.FormDefinitionGenerator;
import com.gitee.mrj.aiboot.core.sf.generators.UIForm;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;

@Component
public class SFOptionalHelpGenerator implements FormDefinitionGenerator {
    @Override
    public void generate(UIForm root, Field field, UIForm node) {
        SFOptionalHelp help = AnnotatedElementUtils.findMergedAnnotation(field, SFOptionalHelp.class);
        ObjectNode ui = node.getUi();
        ObjectNode optionalHelp = putObject(ui, "optionalHelp");

        if (StringUtils.isNotEmpty(help.text())) {
            optionalHelp.put("text", help.text());
        }
        if (StringUtils.isNotEmpty(help.bgColor())) {
            optionalHelp.put("bgColor", help.bgColor());
        }
        if (StringUtils.isNotEmpty(help.icon())) {
            optionalHelp.put("icon", help.icon());
        }
        optionalHelp.put("placement", help.placement().value());
        optionalHelp.put("trigger", help.trigger().value());
        putDouble(optionalHelp, "mouseEnterDelay", help.mouseEnterDelay());
        putDouble(optionalHelp, "mouseLeaveDelay", help.mouseLeaveDelay());
        if (StringUtils.isNotBlank(help.overlayClassName())) {
            optionalHelp.put("overlayClassName", help.overlayClassName());
        }
        KV[] kvs = help.overlayStyle();
        if (ArrayUtils.isNotEmpty(kvs)) {
            ArrayNode overlayStyle = putArray(optionalHelp, "overlayStyle");
            for (KV kv : kvs) {
                overlayStyle.addObject().put(kv.key(), kv.value());
            }
        }


    }


    @Override
    public Class[] supports() {
        return new Class[]{SFOptionalHelp.class};
    }
}
